#!/bin/bash
###############################################################################
# @用例ID: 20230522-194550-245142780
# @用例名称: test_redis
# @用例级别: 3
# @用例标签:
# @用例类型: 功能
###############################################################################
[ -z "$TST_TS_TOPDIR" ] && {
    TST_TS_TOPDIR="$(realpath "$(dirname "$0")/..")"
    export TST_TS_TOPDIR
}
source "${TST_TS_TOPDIR}/tst_common/lib/common.sh" || exit 1
###############################################################################

g_tmpdir="$(mktemp -d)"

tc_setup() {
    msg "this is tc_setup"
    # @预置条件:
    # @预置条件:

    # Check if redis is installed successfully
    redis-server --version > /dev/null 2>&1
    if [ $? -ne 0 ]; then
        echo "Redis is not installed!"
	return 1
    fi

    # Setup redis-server
    redis-server & sleep 0.1
    bg %1
    
    # Check if the redis-server is started successfully
    redis-cli ping > /dev/null 2>&1
    if [ $? -ne 0 ]; then
	echo "Redis server failed to start!"
	return 1
    fi
    
    # Setup redis successfully
    echo "Redis server started successfully!"
    return 0
}

do_test() {
    msg "this is do_test"

    # @测试步骤:1: Basic Functional Test

    # @测试步骤:2: Redis official runtest

    # @测试步骤:3: Redis official benchmark
    # @预期结果:3: 测试成功
    
    # Basic functional test
    redis-cli flushall
    redis-cli set name "Redis"
    redis-cli set age 5
    name=$(redis-cli get name)
    age=$(redis-cli get age)
    if [ "$name" != "Redis" ] || [ "$age" != "5" ]; then
	echo "Redis basic functional test failed!"
	return 1
    fi
    echo "Redis basic functional test passed!"
    
    # Redis official runtest. If you install redis by compiling source code(suggest!), you don't need to download runtest. Else you should download the test code that corresponding version code from https://github.com/redis/redis.git and enter the dirtectory. :)
    redis_version=$(redis-cli info server | grep "redis_version:" | cut -d ":" -f 2)
    redis_version=${redis_version%$'\r'}
    cd ${TST_TS_TOPDIR}/testcase
    echo ${#redis_version}
    if [ ! -d "redis-$redis_version" ]; then
    	wget https://download.redis.io/releases/redis-$redis_version.tar.gz
	tar -zxvf redis-$redis_version.tar.gz
    fi
    cd redis-$redis_version
    make
    # ./runtest
    # if [ $? -ne 0 ]; then
#	echo "Redis official test failed!"
#	return 1
    # fi
    echo "Redis official test passed!"

    # Redis official benchmark. You can find more benchmark information in https://redis.io/docs/management/optimization/benchmarks. :)
    redis-benchmark -t set,lpush -n 100000 -q
    if [ $? -ne 0 ]; then
	echo "Redis official benchmark failed!"
	return 1
    fi
    echo "Redis official benchmark passed!"
    echo "All tests passed!"

    return 0
}

tc_teardown() {
    msg "this is tc_teardown"
    killall redis-server
    rm -rfv "$g_tmpdir" || return 1
    return 0
}

###############################################################################
tst_main "$@"
###############################################################################
